package ru.spbu.math.android.cbrf.application;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.dataprovider.ProviderManager;
import ru.spbu.math.android.cbrf.model.RateType;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class AppConfigureActivity extends Activity {

	private final static String LOG_TAG = "App Configure Activity";

	private CategoryListAdapter categoryListAdapter;
	private static FavoriteListAdapter favoriteListAdapter;

	private final int FULL_MENU = 1;
	private final int EDIT = 2;

	private static Map<String, String> map = new HashMap<String, String>();
	private static boolean isFavoriteList = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		AppConfigure.Empty(this.getApplicationContext());
		map.putAll((Map<String, String>) AppConfigure.loadAllTitlePrefs(this
				.getApplicationContext()));

		if (!map.isEmpty()) {
			Log.d(LOG_TAG, "List is not empty.");
			Log.d(LOG_TAG, "Map size : " + map.size());
			createFavoriteList();
			isFavoriteList = true;
		} else {
			Log.d(LOG_TAG, "Create ProviderManager");
			createCategoryList();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, FULL_MENU, 0, getString(R.string.Full_menu));
		menu.add(0, EDIT, 0, getString(R.string.Favourite));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		Log.d(LOG_TAG, "Create onOptionItemSelected");

		switch (item.getItemId()) {
		case FULL_MENU:
			Log.d(LOG_TAG, "Selected menu");
			setTitle(R.string.Full_menu);
			createCategoryList();
			isFavoriteList = false;
			return true;
		case EDIT:
			Log.d(LOG_TAG, "Selected edit");
			setTitle(R.string.Edit);
			createEditList();
			isFavoriteList = false;
			return true;

		}
		return false;
	}


	@Override 
	public boolean onKeyUp(int keyCode, KeyEvent event){
		super.onKeyUp(keyCode, event); 
		Log.d(LOG_TAG, "KeyCode : " + keyCode);
		if(keyCode == KeyEvent.KEYCODE_BACK){ 
			if(!isFavoriteList &&!map.isEmpty()){ 
				Log.d(LOG_TAG, "Start activity");
//				startActivity(this.getIntent()); 
				startActivity(new Intent(this.getIntent()));
				createFavoriteList();
				isFavoriteList = true; 
			} 
			return true; 
		} 
		return false; 
	}
	
	
	/*
	public void onBackPressed() {
		Log.d(LOG_TAG, "On Back Pressed");
		if (!isFavoriteList && !map.isEmpty()) {
			startActivity(getIntent());
			createFavoriteList();
			isFavoriteList = true;
		}
	}
*/
	private void createFavoriteList() {
		ProviderManager providerManager = ProviderManager.getInstance(this
				.getApplicationContext());
		List<RateType> rateTypes = new ArrayList<RateType>();
		Iterator<String> iterator = map.keySet().iterator();
		String id = "";
		while (iterator.hasNext()) {
			String index = iterator.next();
			Log.d(LOG_TAG, "Index : " + index);
			id = map.get(index);
			Log.d(LOG_TAG, "Id : " + id);
			rateTypes.add(providerManager.getRateTypeByExternalId(id));
		}
		ListView favoriteList = new ListView(this.getApplicationContext());
		favoriteListAdapter = new FavoriteListAdapter(this, rateTypes);
		favoriteList.setAdapter(favoriteListAdapter);
		setTitle(R.string.Favourite);
		setContentView(favoriteList);
	}

	private void createCategoryList() {
		ProviderManager providerManager = ProviderManager.getInstance(this
				.getApplicationContext());
		ListView categoryList = new ListView(this.getApplicationContext());
		categoryListAdapter = new CategoryListAdapter(providerManager
				.getCategories(), this);
		categoryList.setAdapter(categoryListAdapter);
		categoryList.setOnItemClickListener(new OnCategoryClickListener(this,
				(CategoryListAdapter) categoryList.getAdapter()));
		setContentView(categoryList);
	}

	private void createEditList() {
		ProviderManager providerManager = ProviderManager.getInstance(this
				.getApplicationContext());
		ListView categoryList = new ListView(this);
		categoryListAdapter = new CategoryListAdapter(providerManager
				.getCategories(), this);
		categoryList.setAdapter(categoryListAdapter);
		categoryList.setOnItemClickListener(new EditOnCategoryClickListener(
				this, (CategoryListAdapter) categoryList.getAdapter()));
		setContentView(categoryList);
	}

}
